﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Xpedite.Resources;
using Xpedite.WebAbstraction;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Test.Resources
{
  [TestClass]
  public class ResourceUrlTest
  {
    private readonly Mock<IWebRequest> _request = new Mock<IWebRequest>(MockBehavior.Strict);

    public ResourceUrlTest()
    {
      _request.SetupGet(mock => mock.ApplicationPath).Returns("/VirtualDirectory/");
      _request.SetupGet(mock => mock.Url).Returns(new Uri("http://localhost/VirtualDirectory/Pages/MyPage.aspx?key=value"));
    }

    [TestMethod]
    public void TestResourceUrlAsLocalRequest()
    {
      _request.Setup(mock => mock.MapPath("/VirtualDirectory/Pages/MyPage.aspx")).Returns(@"C:\inetpub\wwwroot\VirtualDirectory\Scripts\MyScript.js");

      var resourceUrl = new ResourceUrl(_request.Object);

      Assert.IsTrue(resourceUrl.IsLocal);
      Assert.AreEqual("/VirtualDirectory/Pages/MyPage.aspx?key=value", resourceUrl.VirtualPath);
      Assert.AreEqual(resourceUrl.PhysicalPath, @"C:\inetpub\wwwroot\VirtualDirectory\Scripts\MyScript.js");
    }

    [TestMethod]
    public void TestResourceUrlAsRemoteRelativeUrl()
    {
      var resourceUrl = new ResourceUrl(_request.Object, "../../Scripts/MyScript.js");

      Assert.IsFalse(resourceUrl.IsLocal);
      Assert.IsNull(resourceUrl.PhysicalPath);
      Assert.AreEqual("/Scripts/MyScript.js", resourceUrl.VirtualPath);
    }

    [TestMethod]
    public void TestResourceUrlAsRemoteAbsolutePath()
    {
      var resourceUrl = new ResourceUrl(_request.Object, "/Scripts/MyScript.js");

      Assert.IsFalse(resourceUrl.IsLocal);
      Assert.IsNull(resourceUrl.PhysicalPath);
      Assert.AreEqual("/Scripts/MyScript.js", resourceUrl.VirtualPath);
    }

    [TestMethod]
    public void TestResourceUrlAsRemoteAbsoluteUrl()
    {
      var resourceUrl = new ResourceUrl(_request.Object, "http://localhost/Scripts/MyScript.js");

      Assert.IsFalse(resourceUrl.IsLocal);
      Assert.IsNull(resourceUrl.PhysicalPath);
      Assert.AreEqual("/Scripts/MyScript.js", resourceUrl.VirtualPath);
    }

    [TestMethod]
    public void TestResourceUrlAsRemoteAbsolutePathWithDifferentVirtualDirectory()
    {
      var resourceUrl = new ResourceUrl(_request.Object, new Uri("/AnotherDirectory/Scripts/MyScript.js", UriKind.Relative));

      Assert.IsFalse(resourceUrl.IsLocal);
      Assert.IsNull(resourceUrl.PhysicalPath);
      Assert.AreEqual("/AnotherDirectory/Scripts/MyScript.js", resourceUrl.VirtualPath);
    }

    [TestMethod]
    public void TestResourceUrlAsRemoteAbsoluteUrlWithDifferentVirtualDirectory()
    {
      var resourceUrl = new ResourceUrl(_request.Object, new Uri("http://localhost/AnotherDirectory/Scripts/MyScript.js", UriKind.Absolute));

      Assert.IsFalse(resourceUrl.IsLocal);
      Assert.IsNull(resourceUrl.PhysicalPath);
      Assert.AreEqual("/AnotherDirectory/Scripts/MyScript.js", resourceUrl.VirtualPath);
    }

    [TestMethod]
    public void TestResourceUrlAsRemoteAbsoluteUrlWithDifferentHost()
    {
      var resourceUrl = new ResourceUrl(_request.Object, new Uri("http://remotehost/VirtualDirectory/Scripts/MyScript.js", UriKind.Absolute));

      Assert.IsFalse(resourceUrl.IsLocal);
      Assert.IsNull(resourceUrl.PhysicalPath);
      Assert.AreEqual("http://remotehost/VirtualDirectory/Scripts/MyScript.js", resourceUrl.VirtualPath);
    }

    [TestMethod]
    public void TestResourceUrlAsLocalRelativeUrl()
    {
      _request.Setup(mock => mock.MapPath("/VirtualDirectory/Scripts/MyScript.js")).Returns(@"C:\inetpub\wwwroot\VirtualDirectory\Scripts\MyScript.js");

      var resourceUrl = new ResourceUrl(_request.Object, new Uri("../Scripts/MyScript.js", UriKind.Relative));

      Assert.IsTrue(resourceUrl.IsLocal);
      Assert.AreEqual("/VirtualDirectory/Scripts/MyScript.js", resourceUrl.VirtualPath);
      Assert.AreEqual(@"C:\inetpub\wwwroot\VirtualDirectory\Scripts\MyScript.js", resourceUrl.PhysicalPath);
    }

    [TestMethod]
    public void TestResourceUrlAsLocalAbsolutePath()
    {
      _request.Setup(mock => mock.MapPath("/VirtualDirectory/Scripts/MyScript.js")).Returns(@"C:\inetpub\wwwroot\VirtualDirectory\Scripts\MyScript.js");

      var resourceUrl = new ResourceUrl(_request.Object, new Uri("/VirtualDirectory/Scripts/MyScript.js", UriKind.Relative));

      Assert.IsTrue(resourceUrl.IsLocal);
      Assert.AreEqual("/VirtualDirectory/Scripts/MyScript.js", resourceUrl.VirtualPath);
      Assert.AreEqual(@"C:\inetpub\wwwroot\VirtualDirectory\Scripts\MyScript.js", resourceUrl.PhysicalPath);
    }

    [TestMethod]
    public void TestResourceUrlAsLocalAbsoluteUrl()
    {
      _request.Setup(mock => mock.MapPath("/VirtualDirectory/Scripts/MyScript.js")).Returns(@"C:\inetpub\wwwroot\VirtualDirectory\Scripts\MyScript.js");

      var resourceUrl = new ResourceUrl(_request.Object, new Uri("http://localhost/VirtualDirectory/Scripts/MyScript.js", UriKind.Absolute));

      Assert.IsTrue(resourceUrl.IsLocal);
      Assert.AreEqual("/VirtualDirectory/Scripts/MyScript.js", resourceUrl.VirtualPath);
      Assert.AreEqual(@"C:\inetpub\wwwroot\VirtualDirectory\Scripts\MyScript.js", resourceUrl.PhysicalPath);
    }

    [TestMethod]
    public void TestToUri()
    {
      var resourceUrl = new ResourceUrl(_request.Object, new Uri("http://remotehost/VirtualDirectory/Scripts/MyScript.js", UriKind.Absolute));

      Assert.AreEqual(new Uri("http://remotehost/VirtualDirectory/Scripts/MyScript.js", UriKind.Absolute), resourceUrl.ToUri());
    }

    [TestMethod]
    public void TestToString()
    {
      var resourceUrl = new ResourceUrl(_request.Object, new Uri("http://remotehost/VirtualDirectory/Scripts/MyScript.js", UriKind.Absolute));

      Assert.AreEqual("http://remotehost/VirtualDirectory/Scripts/MyScript.js", resourceUrl.ToString());
    }
  }
}
